package com.wbao.dianniu.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.data.RecReceiveData;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.Utils;

public class RecReceiveAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private List<RecReceiveData> mList;
    private Context mContext;

    public RecReceiveAdapter(Context context) {
        mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (null == mList) {
            mList = new ArrayList<RecReceiveData>();
        }
    }

//	public void setData(List<RecReceiveData> list) {
//		mList = list;
//	}

    public void addData(List<RecReceiveData> list) {
        if (null == mList) {
            mList = new ArrayList<RecReceiveData>();
        }
        for (int i = 0; i < list.size(); i++) {
            mList.add(list.get(i));
        }
    }

    public List<RecReceiveData> getList() {
        return mList;
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (null == mList)
            return 0;
        else
            return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GridHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.receive_job_item, null);
            holder = new GridHolder();
            holder.headIV = convertView.findViewById(R.id.user_head);
            holder.nameTV = convertView.findViewById(R.id.user_name);
            holder.labelTV = convertView.findViewById(R.id.user_label);
            holder.sexIV = convertView.findViewById(R.id.user_sex);
            holder.partnerIV = convertView.findViewById(R.id.user_partner);
            holder.addV = convertView.findViewById(R.id.add_v_iv);
            holder.date = convertView.findViewById(R.id.user_date);
            holder.sex = convertView.findViewById(R.id.receive_job_sex);
            holder.educationTV = convertView.findViewById(R.id.receive_job_education);
            holder.experienceTV = convertView.findViewById(R.id.receive_job_experience);
            holder.postTV = convertView.findViewById(R.id.receive_job_post);
            holder.salaryTV = convertView.findViewById(R.id.receive_job_salary);
            convertView.setTag(holder);
        } else {
            holder = (GridHolder) convertView.getTag();
        }

        RecReceiveData data = mList.get(position);
        if (null != data) {
            if (null != data.getUserInfo()) {
                PictureDownloadUtils.displayHeadImage(mContext, holder.headIV, data.getUserInfo().headPic);
                holder.nameTV.setText(data.getUserInfo().realName == null ? "" : data.getUserInfo().realName);
                holder.labelTV.setText(data.getUserInfo().label == null ? "" : data.getUserInfo().label);
                Utils.showSex(mContext, data.getUserInfo().sex, holder.sexIV);
                Utils.showPartner(mContext, data.getUserInfo().partner, holder.partnerIV);
                Utils.showAuth(data.getUserInfo(), holder.addV);
                if (null != data.getUserInfo().sex && data.getUserInfo().sex == 0) {
                    holder.sex.setText(mContext.getResources().getString(R.string.girl));
                } else {
                    holder.sex.setText(mContext.getResources().getString(R.string.boy));
                }
            } else {
                PictureDownloadUtils.displayHeadImage(mContext, holder.headIV, data.getHeadPic());
                holder.nameTV.setText(data.getRealName() == null ? "" : data.getRealName());
                holder.labelTV.setText(data.getLabel() == null ? "" : data.getLabel());
                holder.sexIV.setVisibility(View.GONE);
                holder.partnerIV.setVisibility(View.GONE);
                holder.addV.setVisibility(View.GONE);
                holder.sex.setText(data.getSexDesc() == null ? "" : "" + data.getSexDesc());
            }
            holder.date.setText(Utils.formatNotifyDate(data.getCreateDate()));
            holder.date.setVisibility(View.VISIBLE);
            holder.experienceTV.setText(data.getExperience() == null ? "" : "" + data.getExperience());
            holder.educationTV.setText(data.getEducation() == null ? "" : "" + data.getEducation());
            holder.postTV.setText(data.getPost() == null ? "" : "应聘：" + data.getPost());
            holder.salaryTV.setText(data.getSalary() == null ? "" : data.getSalary());
        }

        return convertView;
    }

    private class GridHolder extends UserInfoHolder {
        public TextView sex;
        public TextView experienceTV;
        public TextView educationTV;
        public TextView postTV;
        public TextView salaryTV;
    }

}
